package cn.neusoft.luofa603.yuejian_29.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.neusoft.luofa603.yuejian_29.Global;
import cn.neusoft.luofa603.yuejian_29.R;
import cn.neusoft.luofa603.yuejian_29.bean.Training;
import cn.neusoft.luofa603.yuejian_29.bean.Union;

/**
 * Created by Administrator on 2017/10/19.
 */

public class ActivityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;
    private MyItemClickListener mItemClickListener;

    public ActivityAdapter(Context mContext, List mDataList){
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        ImageView activity_pic;
        TextView activity_name;
        TextView ahits;
        private ActivityAdapter.MyItemClickListener mListener;

        public ViewHolder(View itemView,ActivityAdapter.MyItemClickListener myItemClickListener){

            super(itemView);
            activity_pic = (ImageView)itemView.findViewById(R.id.union_img);
            activity_name = (TextView)itemView.findViewById(R.id.union_name);
            ahits=(TextView)itemView.findViewById(R.id.ahits);

            this.mListener =  myItemClickListener;
            itemView.setOnClickListener(this);
        }

        public void onClick(View view) {
            if (mListener != null) {
                mListener.onItemClick(view, getPosition());
            }

        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.item_activity,parent,false);
        ViewHolder holder = new ViewHolder(v, mItemClickListener);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        final Union entity = (Union) mDataList.get(position);
        if (null==entity)
            return;
        ActivityAdapter.ViewHolder viewHolder = (ActivityAdapter.ViewHolder) holder;
        viewHolder.activity_name.setText(entity.getRec_name());
        viewHolder.ahits.setText(entity.getHits());
        String photo = entity.getThumb();
        Picasso.with(mContext)
                .load(Global.PIC+photo)
                .into(viewHolder.activity_pic);

    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public interface MyItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setItemClickListener(MyItemClickListener myItemClickListener) {
        this.mItemClickListener = myItemClickListener;
    }
}
